package com.tong.lib.utils;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.PopupWindow;

public class KeyboardHeighProviderUtils extends PopupWindow implements ViewTreeObserver.OnGlobalLayoutListener {

    private Activity mActivity;
    private final View mRootView;
    private KeyboardListener mKeyboardListener;

    public KeyboardHeighProviderUtils(Activity activity) {
        mActivity = activity;
        mRootView = new View(mActivity);
        setContentView(mRootView);
        setWidth(1);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        mRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setInputMethodMode(INPUT_METHOD_NEEDED);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mActivity.getWindow().getDecorView().post(() -> showAtLocation(mActivity.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 0));
    }

    private Rect rect = new Rect();
    private int mScreenHeight;
    private int mKeyboardHeight;
    private int mCurrentKeyboardHeight;

    @Override
    public void onGlobalLayout() {
        mRootView.getWindowVisibleDisplayFrame(rect);
        if (rect.bottom > mScreenHeight) {
            mScreenHeight = rect.bottom;
        }

        mCurrentKeyboardHeight = mScreenHeight - rect.bottom;
        if (mCurrentKeyboardHeight == mKeyboardHeight) {
            return;
        }

        ValueAnimator valueAnimator = ValueAnimator.ofInt(mKeyboardHeight, mCurrentKeyboardHeight);
        valueAnimator.addUpdateListener(valueAnimator1 -> {
            int value = (int) valueAnimator1.getAnimatedValue();
            if (mKeyboardListener != null) {
                mKeyboardListener.onChange(value,mCurrentKeyboardHeight!=0);
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mKeyboardHeight = mCurrentKeyboardHeight;
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        valueAnimator.setDuration(200).setInterpolator(new DecelerateInterpolator());
        valueAnimator.start();
    }

    public void setKeyboardListener(KeyboardListener keyboardListener) {
        mKeyboardListener = keyboardListener;
    }

    public interface KeyboardListener {
        void onChange(int height,boolean isShow);
    }
}
